<?php
class TotalAction extends SecurityAction {
	
	/*
	'0' => '取消订单',
	'1' => '未支付',
	'3' => '电子版文件错误',
	'4' => '取消订单',
	'7' => '等待印刷中',
	'10' => '已发货',
	'11' => '已完成' 
	 */
	
	function order() {
		
		// 超级管理员权限
		if (session ( "info.rid" ) != '1' && session ( "info.rid" ) != '10') {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
		
		$uid = session("info.uid");
		
		//代理商结构树
		$ub = D('User');
		$map = array ();
		$map[] = array (
			'rid' => array (
				'neq',
				'4'
			)
		);
		

// 		$map [] = array (
// 			'status' => array (
// 				'neq',
// 				'-1'
// 			)
// 		);
		
		$ou = new UserModel ();
		$ulist = $ou->getAllChildrenUser($uid);
		$map[] = array (
				'csl_user.uid' => array (
						'in',
						$ulist
				)
		);
		
		if (empty(session('info.path'))) {
			$map[] = array (
				'path' => array (
					'like',
					'sssssss'
				)
			);
		}elseif(session ( 'info.rid' ) == '1') {
		}else{
			$map[] = array (
				'path' => array (
				'like',
					session ( 'info.path' ) . '%'
				)
			);
		}
		$childrentree = $ub->where ( $map )->order ( 'path asc' )->limit ( 0, 1000 )->getChildrenTree ();
		$this->assign ( 'childrentree', $childrentree );
		
		
		
		//查询当前核心代理商下所属用户的UID
		
		//查询
		if($_GET['name']!='') {//按名字来查询代理商，只查询一条记录
			//查询是否有下属
			$draw = array();
			$draw['name'] = array('like',"%".trim($_GET['name'])."%");
// 			$draw['status'] = 1;
			$dailishang = $ub->where($draw)->find();
			if(!empty($dailishang)) {
				$sql[] = array (
						'csl_order.uid' => $dailishang['uid']
				);
			}else{
				$sql[] = array (
						'csl_order.uid' => 0
				);
			}
		}else if($_GET['name']=='' && $_GET['uid']!='') {
			//查询是否有下属
			$draw = array();
			
			$beuser = $ub->where(array('uid'=>$_GET['uid']))->find();
			$draw['path'] = array('like',$beuser['path']."%");
		//	$draw['status'] = 1;//注释后包含了所有过往的下属
			$xiaji = $ub->where($draw)->limit(10000)->select();
			if(!empty($xiaji)) {
				$temp = array();
				foreach($xiaji as $k=>$v) {
					$temp[] = $v['uid'];
				}
				$sql[] = array (
					'csl_order.uid' => array('in',$temp)
				);
			}else{
				$sql[] = array (
					'csl_order.uid' => $_GET['uid']
				);
			}
		}else{
			$ou = new UserModel ();
			$ulist = $ou->getAllChildrenUser($uid);
			$sql[] = array (
					'csl_order.uid' => array (
							'in',
							$ulist
					)
			);
		}
		
		
		if($_GET['ctime']!='') {
			$sql[] = array (
				'csl_order.ctime' => array('egt',$_GET['ctime'])
			);
		}else{
			$sql[] = array (
				'csl_order.ctime' => array('egt',date('Y-m')."-01")
			);
		}
		if($_GET['utime']!='') {
			$utime = $_GET['utime'];
			if(strlen($utime)==10) {
				$utime .= " 23:59:59";
			}
			$sql[] = array (
				'csl_order.ctime' => array('elt',$utime)
			);
		}else{
			$sql[] = array (
				'csl_order.ctime' => array('elt',date('Y-m-d')." 23:59:59")
			);
		}
		
		if($_GET['paycode']=='1'){
			$sql[] = array (
				'csl_paycode.code_id' => ['egt',1]
			);
		}
		
		
		
		//查询已完成订单
	//	$ob = M( "csl_order" );
		$ob = D("Order");
		$sql[] = array (
			'order_status' => array('EGT',7)
		);
		
		$cpp = 30; // 分页数
		import ( 'ORG.Util.Page' ); // 导入分页类
		$count = $ob->where ( $sql )->count (); // 查询满足要求的总记录数
		$Page = new Page ( $count, $cpp ); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show (); // 分页显示输出
		
		
		$list = $ob
		->join("csl_paycode on csl_paycode.order_no = csl_order.order_no")->field('csl_order.*')
			->where ($sql)
			->order(array('oid'=>'desc'))->limit($Page->firstRow.','.$Page->listRows )->getlist();
		$count = $ob
		->join("csl_paycode on csl_paycode.order_no = csl_order.order_no")->field('csl_order.*')
		->where ($sql)->getcount();
		
// 		dayin($count);
// 		dayin($sql);
	//	dayin($ob->getlastsql());
		
		$this->assign ( "list", $list );
		$this->assign ( 'show', $show ); // 赋值分页输出
		$this->assign ('totalnum', $Page->totalRows);
		$this->assign ( 'count', $count );
		$this->display ( "Total/order" );
	}
	
	
	function yeji(){
	    $year = $_GET["year"];
	    $month = $_GET["month"];
	    $name = $_GET["name"];
	    
	    if (empty($year) || empty($month)){
	        $year = date('Y', time());
	        $month = date('m', time());
	    }
	    $uid = session("info.uid");
// 	    $uid = "44";
	    $db = M("csl_order");
	    
	    //当前登录账户的业绩统计
	    $sqlStr = "select count(order_no) as order_num, sum(order_price) order_price from csl_order where 1=1 ";
	    if (!empty($year) && !empty($month)){
	        $sqlStr .= " and DATE_FORMAT(ctime, '%Y-%m' ) ='".$year."-".$month."' ";
	        
	        $sql[] = " DATE_FORMAT(csl_order.ctime, '%Y-%m' ) ='".$year."-".$month."' ";
	    }
	    if (!empty($uid)){
	        $sqlStr .= " and uid =".$uid;
	    }
	    if (!empty($name)){
	    	 $sql[] = " csl_user.name = '{$name}' ";
	    }
	    
	    //查询当前核心代理商下所属用户的UID
        $ou = new UserModel ();
        $ulist = $ou->getAllChildrenUser($uid); 
        $sql[] = array (
            'csl_user.uid' => array (
                'in',
                $ulist
            )
        );
        
        //只统计已支付订单
        $sql[] = " csl_order.order_status >= 7 ";
        
        $uidStr = '';
        if ($ulist){
            foreach ($ulist as $key => $value){
                $uidStr .= "'".$value."',";
            }
            
            $uidStr = substr($uidStr, 0, strlen($uidStr)-1);
        }
	    
	    //当前账号的订单总计份数，总计金额
	    $info = M()->query($sqlStr);

	    $cpp = 300; // 分页数
	    import ( 'ORG.Util.Page' ); // 导入分页类
	    $count = $db->join("csl_user on csl_order.uid = csl_user.uid ")
	                ->where ($sql)->count(); // 查询满足要求的总记录数
	    $countSql = "  SELECT COUNT(*) AS num from (SELECT COUNT(*) AS tp_count FROM `csl_order` LEFT JOIN csl_user on csl_order.uid = csl_user.uid  
                       WHERE (DATE_FORMAT(csl_order.ctime, '%Y-%m' ) ='".$year."-".$month."'  ) AND ( csl_user.uid in ($uidStr) ) 
                       GROUP BY  csl_order.uid ORDER BY csl_order.ctime desc) a";
	    $count = M()->query($countSql);
	    $Page = new Page ($count[0]['num'], $cpp); // 实例化分页类 传入总记录数和每页显示的记录数
	    $show = $Page->show (); // 分页显示输出
	    
	    //  当前账号下每个核心代理的总计分数，总计金额
        $list = $db
        			->join("csl_user on csl_order.uid = csl_user.uid ")
//         			->join('csl_shopping_cart on csl_shopping_cart.order_no = csl_order.order_no')
	               ->field("sum(csl_order.number) as num, sum(csl_order.order_price) as order_price,csl_user.name,csl_user.uid,csl_order.ctime,csl_user.username,csl_user.mobile ")
	               ->where ($sql)->group(' csl_order.uid')
                   ->order(['num'=>'desc'])
        			->select();
    
        //
        $data = [];
        if(!empty($list)){
        	foreach($list as $value){
        		$data['AllCount'] += $value['num'];
        		$data['Amount']   += $value['order_price'];
        	}
        }
	    
        
	    $this->assign ( "list", $list ); 
	    $this->assign ( 'show', $show ); // 赋值分页输出
	    $this->assign ('info', $info[0]);
	    $this->assign ( 'count', $count);
	    $this->assign ( 'count', $count);
	    $this->assign ( 'data', $data);
	    $this->display ( "Total/yeji" );
	    return;
	}
}

?>